<?php 
class ControllerCheckoutShipping extends Controller {
	var $error = array(); 
 	
  	function index() {
		$address  =& $this->locator->get('address');
		$cart     =& $this->locator->get('cart');
		$config   =& $this->locator->get('config');
		$customer =& $this->locator->get('customer');
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$module   =& $this->locator->get('module');
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$shipping =& $this->locator->get('shipping');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');
				    		
		if (!$customer->isLogged()) {
			$session->set('redirect', $url->ssl('checkout_shipping'));

	  		$response->redirect($url->ssl('account_login'));
    	} 

    	if ((!$cart->hasProducts()) || ((!$cart->hasStock()) && (!$config->get('config_stock_checkout') && ($config->get('config_stock_check'))))) {
	  		$response->redirect($url->ssl('cart'));
    	}

    	if (!$cart->hasShipping()) {
			$session->delete('shipping_address_id');
			$session->delete('shipping_method');
			
			$response->redirect($url->ssl('checkout_payment'));
    	}    	

    	if (!$address->has($session->get('shipping_address_id'))) {
	  		$session->set('shipping_address_id', $customer->getAddressId());
    	}

    	if (!$address->has($session->get('shipping_address_id'))) {
	  		$response->redirect($url->ssl('checkout_address', 'shipping'));
		}
		
		$methods = $shipping->getQuotes();
		
		// If there is only one shipping option, set it to session and go to payment
		//if (count($methods) == 1) {
		//	$keys = array_keys($methods);
		//	$values = array_values($methods);
		//	$method = $values[0]['quote'][$keys[0]]['id'];
		//	$session->set('shipping_method', $method);
		//	$response->redirect($url->ssl('checkout_payment'));
		//}
		
    	$language->load('controller/checkout_shipping.php');

		$template->set('title', $language->get('heading_title')); 
  
    	if ($request->isPost() && $request->has('shipping', 'post') && $this->validate()) {
			$session->set('shipping_method', $request->gethtml('shipping', 'post'));
			
			$session->set('comment', strip_tags($request->gethtml('comment', 'post')));

	  		$response->redirect($url->ssl('checkout_payment'));
    	}
		
    	$view = $this->locator->create('template');

		$view->set('tpl', $template);

    	$view->set('heading_title', $language->get('heading_title'));

    	$view->set('text_shipping', $language->get('text_shipping'));
    	$view->set('text_shipping_to', $language->get('text_shipping_to'));
    	$view->set('text_shipping_address', $language->get('text_shipping_address'));
    	$view->set('text_shipping_method', $language->get('text_shipping_method'));
    	$view->set('text_shipping_methods', $language->get('text_shipping_methods'));
    	$view->set('text_comments', $language->get('text_comments'));
    
		$view->set('button_change_address', $language->get('button_change_address'));
    	$view->set('button_back', $language->get('button_back'));
    	$view->set('button_continue', $language->get('button_continue'));
    
		$view->set('error', @$this->error['message']);
    
		$view->set('action', $url->ssl('checkout_shipping'));
    		
    	$view->set('address', $address->getFormatted($session->get('shipping_address_id'), '<br />'));
    
    	$view->set('change_address', $url->ssl('checkout_address', 'shipping'));
    	
    	$view->set('methods', $methods);

    	$view->set('default', $session->get('shipping_method'));
		
    	$view->set('comment', $session->get('comment'));

    	$view->set('back', $url->href('cart'));
		
		$template->set('content', $view->fetch('content/checkout_shipping.tpl'));

		$template->set($module->fetch());
	
    	$response->set($template->fetch('layout.tpl'));
  	}
  
  	function validate() {
		$language =& $this->locator->get('language');
		$request  =& $this->locator->get('request');
			    
    	if (!$request->gethtml('shipping', 'post')) {
	  		$this->error['message'] = $language->get('error_shipping');
		}
	
		if (!$this->error) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}
  	}
}
?>
